
<template>
    <div class="container_table">
        <a-table
            :columns="columns"
            :dataSource="dataSource"
            :pagination="false"
            :loading="loading"
        >
            <template #bodyCell="{ column, record }">
                <template v-if="column.dataIndex === 'action'">
                    <span>
                        <a @click="handleUpdate(record)">修改</a>
                        <a-divider type="vertical" />
                        <a-popconfirm
                            title="确认删除此条数据？"
                            ok-text="确认"
                            cancel-text="取消"
                            @confirm="handleConfirmDelete(record)"
                        >
                            <a class="warning_link">删除</a>
                        </a-popconfirm>
                    </span>
                </template>
            </template>
        </a-table>
    </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { message } from 'ant-design-vue';
import { Bus } from '@/plugin';
// Component
import Icon from '@/component/common/Icon.vue';
// Api
import Api from '@/axios/api/org';
// Model
import { Result } from "@/model/common";
import { OrgQuery, OrgListDTO } from '@/model/org';

onMounted(() => {
    Bus.on('refresh_org', (_query: any) => {
        if (_query) {
            query = _query;
        }
        refresh();
    })
});

// 定义事件
const emit = defineEmits(['update']);

// 变量
const loading = ref(false);
let query = new OrgQuery();
const columns = ref(OrgListDTO.columns);
const dataSource = ref<OrgListDTO[]>([]);

// 方法
const refresh = () => {
    loading.value = true;
    Api.pageList(query)
        .then((res: Result) => {
            dataSource.value = res.data;
        })
        .finally(() => {
            loading.value = false;
        });
}

/**
 * 点击更新按钮事件
 */
const handleUpdate = (record: OrgListDTO) => {
    emit('update', record);
}

/**
 * 确认删除事件
 */
const handleConfirmDelete = (record: OrgListDTO) => {
    loading.value = true;
    Api.delete(record.orgId)
        .then(() => {
            message.success('删除成功！')
            refresh();
        })
        .catch((err: Result) => {
            message.error(`删除失败！${err.msg}`);
        })
        .finally(() => {
            loading.value = false;
        });
}

// 定义组件对外暴露的内容
defineExpose({
    refresh
})

</script>

<style lang="less" scoped>
</style>
